package com.doghome.news.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.doghome.news.annotation.CheckPermission;
import com.doghome.news.entity.Topic;
import com.doghome.news.service.TopicService;
import com.doghome.news.utils.BizCode;
import com.doghome.news.utils.BizDto;

@RestController
public class TopicController {
	
	@Autowired
	private TopicService topicService;
	
	@GetMapping("/allTopic")
	public BizDto<List<Topic>> allTopic(Integer tid){
		return new BizDto<List<Topic>>(BizCode.SUCCESS, "success", topicService.allTopic(tid));
	}
	
	@GetMapping("/checkTopicName")
	public BizDto<Boolean> checkTopicName(String tname){
		return new BizDto<Boolean>(BizCode.SUCCESS,"success",topicService.repeateName(tname));
	}
	
	@PostMapping("/addTopic")
	@CheckPermission(permission = {1})
	public BizDto<Boolean> addTopic(@RequestBody Topic topic){
		return new BizDto<Boolean>(BizCode.SUCCESS,"success",topicService.addTopic(topic));
	}
	
	@PostMapping("/modifyTopic")
	@CheckPermission(permission = {1})
	public BizDto<Boolean> modifyTopic(@RequestBody Topic topic){
		return new BizDto<Boolean>(BizCode.SUCCESS,"success",topicService.modifyTopic(topic));
	}
	
	@GetMapping("/delTopic")
	@CheckPermission(permission = {1})
	public BizDto<Boolean> delTopic(Integer tid){
		BizDto<Boolean> bizDto = new BizDto<Boolean>();
		
		switch (topicService.delTopic(tid)) {
		case 1:
			bizDto.setCode(BizCode.SUCCESS);
			bizDto.setMsg("主题删除成功");
			bizDto.setData(true);
			break;
		case 3:
			bizDto.setCode(BizCode.ERROR);
			bizDto.setMsg("主题下还有新闻不能删除");
			bizDto.setData(false);
			break;
		default:
			bizDto.setCode(BizCode.ERROR);
			bizDto.setMsg("主题删除失败");
			bizDto.setData(false);
			break;
		}
		return bizDto;
	}

}
